<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['wxgid']){
	$id=pdo_getcolumn('core_account_wxapp',array('id'=>$_RQ['wxgid'],'uniacid'=>$_SESSION['uniacid']),'plugin_key');
}
$_RQ['id']=$_RQ['id']?:$_RQ['merchid'];
$_RQ['id']=$_RQ['id']?:$_RQ['onemerchid'];
if($_RQ['id'] && !$id){
	$id=$_RQ['id'];
}
$_SESSION['current_plugin']='jy_merchfuwu';
$_SESSION['indexurl']='jy_merchfuwu/merchshop/';
$id=$id?:$_SESSION['merchid'];
$id=$id?:$_SESSION['onemerchid'];
$mid=$_RQ['fromuser']?:$_RQ['mid'];
$mid=$mid?:$_SESSION['uid'];
$return['params']=array();
if($_RQ['op']=='main'){
	if($id){
		$myshop=pdo_get('jy_merch_user',array('id'=>$id));
	}else{
		exi('无法访问小店，没有小店ID','error');
	}
	$_SESSION['merchid']=$_SESSION['onemerchid']=$myshop['id'];
	$_SESSION['onemerchurl']='jy_merchfuwu/merchshop//id='.$myshop['id'];
	model('jy_weishop/member')->checklogin('','','',$myshop['id']);
	model('jy_merch/member')->up_catefans($myshop['id']);
    model('jy_merch/member')->addmember($myshop['id'],'',true);
	$return['merchmember']=pdo_get('jy_merch_member',array('merchid'=>$myshop['id'],'mid'=>$_SESSION['uid']));
	$return['member']=cfc('core')->member_show($_SESSION['uid']);
	$groupset=model('jy_merch/model')->get_groupset($_SESSION['onemerchid']);
	$return['pluginset']['footernav']=$groupset['menus']?:$return['pluginset']['footernav'];
	if($_SESSION['onemerchid']){
        $merchset=$this->plugin_setting('merchset'.$_SESSION['onemerchid']);
        $return['pluginset']['footernav']=$merchset['footernav']?:$return['pluginset']['footernav'];
    }
	$_SESSION['merchfooternav']=$return['pluginset']['footernav'];
	$return['merchset']=$this->plugin_setting('merchset'.$_SESSION['onemerchid']);
	$return['setting']=array(
		'hdps'=>$return['merchset']['merchhdps']?:$return['pluginset']['merchhdps'],
		'bankuai1'=>$groupset['bankuai']?:$return['pluginset']['bankuai1'],
		'indexcube'=>$return['merchset']['indexcube'],
	);
	$goodsid=intval($_RQ['goodsid']);
	$return['fromgoods']=array();
	if($goodsid){
		$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
		$return['fromgoods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),$fields);
		$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
		$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
		$return['fromgoods']['cartid']=$cartinfo['id']?:0;
		$return['fromgoods']['carttotal']=$cartinfo['total']?:0;
		$return['fromgoods']['cartprice']=$cartinfo['price']?:0;
	}
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$myshop['out_price']=$myshop['out_price']>0?$myshop['out_price']:$citypeisong['out_price'];
	if(!$myshop['type']){
		$myshop['out_price']=0;
	}
	$myshop['goodscount']=pdo_count('jy_weishop_goods',array('merchid'=>$myshop['id']));
	$myshop['ordercount']=pdo_count('jy_weishop_order',array('merchid'=>$myshop['id']));
	$myshop['orderfee']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$myshop['id']),'price');
	$myshop['lat_lng']=json_decode($myshop['lat_lng'],true);
	$myshop['onlinetime']=$myshop['onlinetime']?json_decode($myshop['onlinetime'],true):array('star'=>'07:00','end'=>'22:00');
	$myshop['onlinetime']['star']=$myshop['onlinetime']['start']?:'7:00';
	$myshop['onlinetime']['end']=$myshop['onlinetime']['end']?:'22:00';	
	$days=date('Y-m-d',TIMESTAMP);
	$star=strtotime($days.' '.$myshop['onlinetime']['star']);
	$end=strtotime($days.' '.$myshop['onlinetime']['end']);
	if($citypeisong['isopen'] && !$citypeisong['is_dispatch'] && ($star>TIMESTAMP || TIMESTAMP>$end)){
		$myshop['offline']=1;
	}	
	$goodsids=pdo_getall('jy_weishop_goods',array('merchid'=>$myshop['id']),'id','id');
	if($goodsids){
		$goodsids=array_keys($goodsids);
		$myshop['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsids),'tags');
	}
	
	$tags=str_array($return['pluginset']['tags'],'|');
	$key=mt_rand(0,count($tags));
	$myshop['tags']=$myshop['tags']?:$tags[$key];
	$myshop['coupons']=model('jy_weishop/coupon')->getmerchcoupon($myshop['id'],1);
	$myshop['logo']=tomedia($myshop['logo']);
	$return['kefu']=$myshop['iskefu'];
	$return['params']=array('page'=>1,'type'=>0);
	//补充
	$category=pdo_getall('jy_weishop_goods_group',array('mid'=>$myshop['mid'],'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\' ','uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0,'is_fuwu'=>0),'','id','paixu desc');
	if(empty($category)){
		$category=pdo_getall('jy_weishop_goods_group',array('mid'=>0,'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\' ','uniacid'=>$_SESSION['uniacid'],'parentid'=>0,'is_fuwu'=>0),'','id','paixu desc');
	}else{
		$ismygroups=true;
	}
	$getcarts=model('jy_weishop/cart')->getall(1,$myshop['id'],true);
	$carts=$getcarts['list'];
	$allnum=$getcarts['alltotal'];
	$goodsprice=$getcarts['goodsprice'];
	foreach($category as $k=>&$v){
		$allgoods=pdo_getall('jy_weishop_goods',array('merchid'=>$myshop['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>$v['id']),'id');
		$v['total']=0;
		foreach($allgoods as $ag){
			$carttotal=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$v['total'] +=$carttotal;
		}
		if(empty($allgoods) && !$ismygroups){
			unset($category[$k]);
		}
	}
	paixu($category,'id','asc');
	//运费计算：
	$order=array(
		'list'=>$carts,
		'total_price'=>$goodsprice,
	);
	$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id'],$myshop);
	$return['dispatchprice']=$up_dispatch['get_freight']['fee'];
	$allprice = $goodsprice - $youhui;
	$return['category']=$category;
	$return['nowckey']=0;
	$return['allnum']=$allnum;
	$return['totalprice']=$allprice;
	$return['carts']=$carts;
	$return['params']['pid']='0';
	$sharelink='jy_merchfuwu/merchshop//id='.$myshop['id'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$myshop['merchname'],
			'desc'=>$myshop['desc']?$myshop['desc']:$myshop['merchname'].'的小店，欢迎光临！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($myshop['logo'])
	);
	$return['myshop']=$myshop;
	set_html('title',$myshop['merchname']);
	exi($return);
}
if($_RQ['op']=='list'){
	$merchid=intval($_RQ['merchid']);
	$merchid=$merchid?:$_SESSION['merchid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>4,'status'=>1,'nochecked'=>0,'merchid'=>$merchid,'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\' ');
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if(isset($_RQ['pcate']) && $_RQ['pcate']!='all'){
		$params['pcate']=intval($_RQ['pcate']);
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	if($_RQ['keyword']){		
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['title']=$_RQ['keyword'];	
	}
		//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 6;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
	$orderby='paixu desc ,salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales' || $_RQ['order']==0){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']==1){
			$params['saletype likes']=array('1');
		}
		if($_RQ['order']==2){
			$params['saletype likes']=array('3');
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}
	if($list){
		foreach($list as &$itemv){
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				//已选规格
				$selectspecs = array();
				$selespename = '';
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							$selectspecs[$k]=$sp['items'][0]['id'];
							$selespename .=$sp['items'][0]['title'].'，';
						}
				}
				//默认已选参数：
				$spsids = '';
				foreach($selectspecs as $k=>&$s){
						if($k==0){
							$spsids .= $s;
						}else{
							$spsids .= '_'.$s;
						}
				}
				if($_SESSION['store_id']){
					$getinfo=model('jy_weishop/store')->getinfo($itemv['id'],$_SESSION['store_id']);
					$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
					$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
					$itemv['options']=model('jy_weishop/store')->getoption($itemv['id'],$_SESSION['store_id'],$spsids);
				}
				if(!$itemv['options']['id']){
					$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
				}
				//默认商品规格价格：
				if($itemv['options']){
					$itemv['price']=$itemv['options']['price'];
					if($itemv['active']['discount']>10){
						$itemv['options']['oldprice']=$itemv['options']['price'];
						$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
						$itemv['options']['price']=$itemv['price'];
					}
					$itemv['post'][$itemv['options']['id']]=array('num'=>0,'title'=>$selespename);
					$op_carts=pdo_getall('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),array('id','optionid','total'));
					foreach($op_carts as $ov){
						$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$ov['optionid']),'specs');
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$optitle=implode("，",$specs);
						$itemv['post'][$ov['optionid']]=array('num'=>$ov['total']?:0,'title'=>$optitle);
					}
				}
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$itemv['optionid']=$itemv['options']['id'];
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['red_fee']=model('jy_merch/model')->red_fee($itemv['price']);
		}
	}
	exi($list);
}
if($_RQ['op']=='getcomments'){
	$merchid=intval($_RQ['merchid']);
	$return['list']=model('jy_weishop/goods')->get_comments(0,6,$comment_total,$_RQ['type'],$_RQ['page'],$merchid);
	exi($return);
}
if($_RQ['op']=='get_juli'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$merchid=intval($_RQ['merchid'])?:$_SESSION['merchid'];
	$storelat_lng=pdo_getcolumn('jy_merch_user',array('id'=>$merchid),'lat_lng');
	$storelat_lng=json_decode($storelat_lng,true);
	$juli=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$storelat_lng['lng'],$storelat_lng['lat']);
	$juli=intval($juli/1000);
	$juli=$juli?:0;
	exi($juli);
}
if($_RQ['op']=='like'){
	$postdata=get_postdata();
	$merchid=$postdata['merchid'];
	if(!$merchid){
		exi('没有找到该商户，请重新选择！','error');
	}
	$looklist=pdo_get('jy_merch_like_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$merchid));
	if($looklist){
		if($looklist['islike'] !=1){
			pdo_update('jy_merch_user',array('likesnum +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
			pdo_update('jy_merch_like_log',array('islike'=>1),array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			exi('收藏成功！');
		}else{
			pdo_update('jy_merch_user',array('likesnum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
			pdo_update('jy_merch_like_log',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$merchid));
			exi('取消收藏成功！');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$merchid,
			'mid'=>$_SESSION['uid'],
			'islike'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_merch_like_log',$insert);
		exi('收藏成功！');
	}
}
if($_RQ['op']=='up_fans'){
	$postdata=get_postdata();
	if($_RQ['isfail']){
		$_SESSION['isshow_fansxieyi']=1;
		exi('取消成功！');
	}
	model('jy_merch/member')->up_catefans($postdata['storeid']);
	model('jy_merch/member')->up_addmember($postdata['storeid']);
	exi('绑定成功！');
}
?>